package com.erp.gather.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.gather.domain.ErpCollectProductOptimize;
import com.erp.gather.domain.bo.ErpCollectProductOptimizeBo;
import com.erp.gather.domain.vo.ErpCollectProductOptimizeVo;
import com.erp.gather.domain.vo.ErpProductAtlasTagVo;
import com.erp.gather.service.IErpCollectProductOptimizeService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 采集商品优化
 *
 * @author erp
 * @date 2023-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/collectProductOptimize")
public class ErpCollectProductOptimizeController extends BaseController {

    private final IErpCollectProductOptimizeService iErpCollectProductOptimizeService;

    /**
     *批量确认优化
     */
    @GetMapping("/batchOptimization")
    public R<Object> batchOptimization(
            @Param("collectProductIds") String collectProductIds
    ) {
        List<String> list = Arrays.asList(collectProductIds.split(","));
        if (list.size() == 0){
            return R.fail("没有需要处理的商品");
        }
        return iErpCollectProductOptimizeService.batchOptimization(list);
    }

    /**
     * 复制被修改的公共产品
     */
    @GetMapping("/copyCollectProductPic")
    public void copyCollectProductPic(
            @Param("oldCollectId") Long oldCollectId,
            @Param("newCollectId") Long newCollectId
    ) {
        iErpCollectProductOptimizeService.copyCollectProductPic(oldCollectId,newCollectId);
    }

    /**
     * 将商品转为未优化状态
     */
    @GetMapping("/makeProductNotOptimize")
    public R<Object> makeProductNotOptimize(
            @Param("collectProductId") Long collectProductId
    ) {
        return iErpCollectProductOptimizeService.makeProductNotOptimize(collectProductId);
    }

    /**
     * 数据调换
     */
    @PostMapping("/change")
    public void change() {
        iErpCollectProductOptimizeService.change();
    }

    /**
     * 轮询修改
     */
    @PostMapping("/updateTagIds")
    public R<String> updateTagIds() {
        List<ErpCollectProductOptimizeVo> erpCollectProductOptimizeList = iErpCollectProductOptimizeService.queryList(new ErpCollectProductOptimizeBo());
        StringBuffer tageStr = new StringBuffer();
        ErpCollectProductOptimizeBo erpCollectProductOptimizeBo = new ErpCollectProductOptimizeBo();
        for (ErpCollectProductOptimizeVo product : erpCollectProductOptimizeList) {
            //获取tagIds，将数组转为list
            if (!product.getTagIds().startsWith("[{")) {
                continue;
            }
            List<ErpProductAtlasTagVo> erpProductAtlasTags = JSONArray.parseArray(product.getTagIds(), ErpProductAtlasTagVo.class);
            for (ErpProductAtlasTagVo tag : erpProductAtlasTags) {
                tageStr.append(tag.getId());
                tageStr.append(",");
            }
//            String tagids = product.getTagIds().substring(0, product.getTagIds().length() - 1);

            product.setTagIds(tageStr.toString().substring(0, tageStr.length() - 1));
//            System.out.println();
            BeanUtils.copyProperties(product, erpCollectProductOptimizeBo);

            iErpCollectProductOptimizeService.updateByBo(erpCollectProductOptimizeBo);
            tageStr = new StringBuffer();
        }
        return R.ok("成功");
    }

    /**
     * 保存单条记录
     */
    @PostMapping("/addOptimizePic")
    public R<Object> addOptimizePic(@RequestBody String msg) {
        JSONObject jsonObject = JSONObject.parseObject(msg);
        List<ErpCollectProductOptimize> jsonArray = JSONArray.parseArray(jsonObject.getString("list"), ErpCollectProductOptimize.class);
        String collectProductId = jsonObject.getString("collectProductId");
        String type = jsonObject.getString("type");
        Integer isSave = jsonObject.getInteger("isSave");
        return iErpCollectProductOptimizeService.addOptimizePic(jsonArray, type, collectProductId, isSave);
    }

    /**
     * 显示图集详情
     */
    @GetMapping("/getOptimizePicByCollectProductId")
    public R<Map<String, Object>> getOptimizePicByCollectProductId(Long collectProductId, Integer type) {
        return iErpCollectProductOptimizeService.getOptimizePicByCollectProductId(collectProductId, type);
    }



    /**
     * 查询采集商品优化列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpCollectProductOptimizeVo> list(ErpCollectProductOptimizeBo bo, PageQuery pageQuery) {
        return iErpCollectProductOptimizeService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取采集商品优化详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ErpCollectProductOptimizeVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable Long id) {
        return R.ok(iErpCollectProductOptimizeService.queryById(id));
    }

    /**
     * 新增采集商品优化
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpCollectProductOptimizeBo bo) {
        return toAjax(iErpCollectProductOptimizeService.insertByBo(bo));
    }

    /**
     * 修改采集商品优化
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpCollectProductOptimizeBo bo) {
        return toAjax(iErpCollectProductOptimizeService.updateByBo(bo));
    }

    /**
     * 删除采集商品优化
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpCollectProductOptimizeService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 跑一边计划
     */
    @GetMapping("processingProductsWithUpdatedImages")
    public R<Object> processingProductsWithUpdatedImages() {
        return iErpCollectProductOptimizeService.processingProductsWithUpdatedImages();
    }

}
